import Vue from 'vue'
import Vuex from 'vuex'
import consts from '@/service/consts'

import { getInfo } from '@/service/api'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: ''
  },
  mutations: {
    ADD_USER(state, user) {
      state.userInfo = user
    },
    REMOVER_USER(state) {
      state.userInfo = ''
      localStorage.removeItem(consts.token)
    },
    ADD_TOKEN(state, token) {
      localStorage.setItem(consts.token, token)
    }
  },
  actions: {
    addUser({ commit }, user) {
      commit('ADD_USER', user)
    },
    removerUser({ commit }) {
      commit('REMOVER_USER')
    },
    addToken({ commit }, token) {
      console.log(444444, token)
      commit('ADD_TOKEN', token)
    },
    getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          commit('ADD_USER', res.data)
          resolve(res.data)
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
  modules: {
  }
})
